#include "titlebar.h"

#include <QHBoxLayout>
#include <QApplication>
#include <QStyle>
#include <QDebug>
#include <QStyleOption>
#include <QPainter>
#include <QMouseEvent>

#if defined (Q_OS_WINDOWS)
// Windows环境
#include <qt_windows.h>
#endif

TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
    initGUI();
}

void TitleBar::initGUI()
{
    setAttribute(Qt::WA_StyledBackground); // 父窗口不影响子窗口的样式
//    setAttribute(Qt::WA_TranslucentBackground);  //设置窗口背景透明
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);  //去掉窗口边框

    this->setStyleSheet("QWidget{background-color:#FFFFFF;  \
            border-top-left-radius:10px;   \
            border-top-right-radius:10px; \
            border-bottom-left-radius:0px; \
            border-bottom-right-radius:0px; \
            }");
    setFixedHeight(32 + 5 * 2);

    logo = new QLabel(this);
    logo->setFixedSize(32,32);
    logo->setPixmap(QApplication::style()->standardPixmap((enum QStyle::StandardPixmap) 15 ));

    title = new QLabel(this);
    title->setFixedSize(32,32);
    title->setText("标题");

    minbtn = new QPushButton(this);
    minbtn->setFixedSize(32,32);
    minbtn->setIcon(QApplication::style()->standardIcon((enum QStyle::StandardPixmap) 1 ));
    minbtn->setStyleSheet("QPushButton{border:none}" \
                          "QPushButton:hover{" \
                          "background-color:rgb(99,99,99);" \
                          "border-radius:10px;" \
                          "border:none}");

    maxbtn = new QPushButton(this);
    maxbtn->setFixedSize(32,32);
    maxbtn->setIcon(QApplication::style()->standardIcon((enum QStyle::StandardPixmap) 2 ));
    maxbtn->setStyleSheet("QPushButton{border:none}" \
                          "QPushButton:hover{" \
                          "background-color:rgb(99,99,99);" \
                          "border-radius:10px;" \
                          "border:none}");

    setbtn = new QPushButton(this);
    setbtn->setFixedSize(32,32);
    setbtn->setIcon(QApplication::style()->standardIcon((enum QStyle::StandardPixmap) 4 ));
    setbtn->setStyleSheet("QPushButton{border:none}" \
                          "QPushButton:hover{" \
                          "background-color:rgb(99,99,99);" \
                          "border-radius:10px;" \
                          "border:none}");

    closebtn = new QPushButton(this);
    closebtn->setFixedSize(32,32);
    closebtn->setIcon(QApplication::style()->standardIcon((enum QStyle::StandardPixmap) 3 ));
    closebtn->setStyleSheet("QPushButton{border:none}" \
                            "QPushButton:hover{" \
                            "background-color:rgb(99,99,99);" \
                            "border-radius:10px;" \
                            "border:none}");

    connect(setbtn, &QPushButton::clicked, this, &TitleBar::OnClocked);
    connect(minbtn, &QPushButton::clicked, this, &TitleBar::OnClocked);
    connect(maxbtn, &QPushButton::clicked, this, &TitleBar::OnClocked);
    connect(closebtn, &QPushButton::clicked, this, &TitleBar::OnClocked);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(logo);
    layout->addWidget(title);

    layout->addStretch();

    layout->addWidget(minbtn);
    layout->addWidget(maxbtn);
    layout->addWidget(setbtn);
    layout->addWidget(closebtn);

    layout->setContentsMargins(5,5,5,5);
}

void TitleBar::OnClocked()
{
    QPushButton* pbutton = qobject_cast<QPushButton*>(sender());
    QWidget *pwindow = this->window();

    if (pbutton == minbtn){
        qDebug() << "minbtn";
        pwindow->showMinimized();
    } else if (pbutton == maxbtn) {
        if (pwindow->isMaximized()) {
            pwindow->showNormal();
        } else {
            pwindow->showMaximized();
        }
    } else if (pbutton == setbtn) {

    } else if (pbutton == closebtn) {
        emit signalClose();
    } else {

    }
}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    maxbtn->click();
}

void TitleBar::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    this->style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

#if defined (Q_OS_LINUX) && !defined (Q_OS_ANDROID)
void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    qDebug() << "diff  pos" << diffpos;
    QPoint pos = event->globalPos();
    this->parentWidget()->parentWidget()->move(pos - diffpos);  // 计算出新应用左上角的位置
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    mousepos = event->globalPos(); // 鼠标当前的全局位置
    winpos = this->parentWidget()->parentWidget()->pos(); // 父窗口当前的全局位置
    diffpos = mousepos - winpos;  // 鼠标在应用中的位置
}
#endif


#if defined (Q_OS_WINDOWS)
// Windows环境
void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if (ReleaseCapture()) {
        QWidget *pwindow = this->window();
        if (pwindow->isTopLevel()){
            SendMessage(HWND(pwindow->winId()), WM_SYSCOMMAND, SC_MOVE+HTCAPTION, 0);
        }
    }
//    event->ignore();
}
#endif
